/*
 * 表单验证，自动混入到Form表单
 */

export const Mixin = {
  methods: {
    // 邮箱验证
    email(rule, value, callback) {
      let val = this.formData[this.config.prop];
      if (!val || val === "") {
        callback();
      } else {
        const regex =
          /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; //使用此正则即可
        if (!regex.test(val)) callback(new Error("请输入正确身份证格式"));
        else callback();
      }
    },
    // 身份证验证
    idCard(rule, value, callback) {
      let val = this.formData[this.config.prop];
      if (!val || val === "") {
        callback();
      } else {
        const regex =
          /^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$/; //使用此正则即可
        if (!regex.test(val)) callback(new Error("请输入正确身份证格式"));
        else callback();
      }
    },
    // 手机号验证
    phone(rule, value, callback) {
      let val = this.formData[this.config.prop];
      if (!val || val === "") {
        callback();
      } else {
        const regex = /^1[3456789]\d{9}$/; //使用此正则即可
        if (!regex.test(val)) callback(new Error("请输入正确手机格式"));
        else callback();
      }
    },
    // 整数验证
    int(rule, value, callback) {
      let val = this.formData[this.config.prop];
      let flag = Number.isInteger(Number(val));
      if (!flag) {
        return callback(new Error("请输入整数数值"));
      } else {
        callback();
      }
    },
    // 整数验证或浮点数
    number(rule, value, callback) {
      let val = this.formData[this.config.prop];
      if (!val) return callback();
      if (!Number(val) && Number(val) != 0) {
        return callback(new Error("请输入数值"));
      } else {
        callback();
      }
    },
    // 输入框字数限制
    limit(rule, value, callback) {
      let val = this.formData[this.config.prop];
      let limit = this.config.type == "ZInput" ? 50 : 200;
      if (
        val &&
        val.length.length ==
          (this.config.maxlength ? this.config.maxlength : limit)
      ) {
        return callback(new Error(`已到达到最多字符输入限制`));
      } else {
        callback();
      }
    },
    // 是否必填
    isRequired(rule, value, callback) {
      let val = this.formData[this.config.prop];
      if (this.config.prop.constructor === Object) {
        val = this.formData[this.config.prop.value];
      } else {
        val = this.formData[this.config.prop];
      }
      if (
        this.formData[this.config.prop] &&
        typeof this.formData[this.config.prop] == "string"
      ) {
        val = this.formData[this.config.prop].trim();
      }
      if (val && val.constructor === Array && !val.length) {
        return callback(new Error(`请选择${this.config.name}`));
      }
      if (!val) {
        let err = "请填写";
        if (this.config.type == "ZUpload" || this.config.type == "ZImageUpload")
          err = "请上传";
        return callback(new Error(`${err}${this.config.name}`));
      } else {
        callback();
      }
    },
  },
  created() {
    if (this.config.type == "ZInput" || this.config.type == "ZTextarea") {
      this.rules.push({
        validator: this.limit,
        trigger: "blur",
      });
    }
    if (this.config.required) {
      this.rules.push({
        validator: this.isRequired,
        trigger: "blur",
      });
    }
    if (this.config.type == "ZNumber") {
      this.rules.push({
        validator: this.number,
        trigger: "change",
      });
    }
    if (this.config.validate) {
      this.config.validate.forEach((item) => {
        this.rules.push({
          validator: this[item],
          trigger: "blur",
        });
      });
    }
  },
};
